package com.healthcloud.service.impl;

import com.healthcloud.dao.BusInstrumentDao;
import com.healthcloud.dao.BusRentDao;
import com.healthcloud.entity.*;
import com.healthcloud.service.BusRentService;
import com.healthcloud.service.PpPatientBaseService;
import com.healthcloud.utils.CommonMethod;
import com.healthcloud.utils.PageParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BusRentServiceImpl implements BusRentService {

    @Autowired
    private BusRentDao busRentDao;

//	@Autowired
//	private LogMapper logMapper;

    @Autowired
    private BusInstrumentDao busInstrumentDao;

    @Autowired
    private CommonMethod commonMethod;

    @Autowired
    private PpPatientBaseService ppPatientBaseService;

    @Override
    public List<BusRentEntity> search(BusRentEntity entity, PageParam pageParam) {
        return busRentDao.queryList(entity, pageParam);
    }

    @Override
    public List<BusRentEntity> queryList(BusRentEntity entity, PageParam pageParam) {
        return busRentDao.queryList(entity, pageParam);
    }

    @Override
    public int count(BusRentEntity entity) {
        return busRentDao.queryTotal(entity);
    }

    @Override
    public int queryTotal(BusRentEntity entity) {
        return busRentDao.queryTotal(entity);
    }

    @Override
    public int createRent(BusRentEntity entity) {
        // TODO Auto-generated method stub
        int i = busRentDao.createRent(entity);
        if (i > 0) {
            //把设备置为已租
            BusInstrumentEntity bean = new BusInstrumentEntity();
            bean.setInstrumentId(entity.getInstrumentId());
            bean.setIdCard(entity.getIdCard());
            bean.setInstrumentStatus("2");
            bean.setIntegralHospId(entity.getHospId());
            busInstrumentDao.updateStatus(bean);
        }
        //添加病人信息
        addPatient(entity);
        return i;
    }

    private void addPatient(BusRentEntity rent) {
        PpPatientBaseEntity patient = ppPatientBaseService.queryByIdcard(rent.getIdCard());
        if (patient==null) {//当没有病人信息时添加病人
            patient = new PpPatientBaseEntity();
            patient.setIdcard(rent.getIdCard());
            patient.setPhone(rent.getRentTel());
            patient.setRealname(rent.getRentMan());
            patient.setGender(CommonMethod.idCardToGender(rent.getIdCard()));
            patient.setType(1);
            ppPatientBaseService.add(patient);
        }
    }

    @Override
    public int updateBack(BusRentEntity entity) {
        // TODO Auto-generated method stub
        int i = busRentDao.updateBack(entity);
        BusRentEntity oldEntity = busRentDao.searchById(entity.getRentId());
        if (i > 0) {
            BusInstrumentEntity bean = new BusInstrumentEntity();
            bean.setInstrumentId(oldEntity.getInstrumentId());
            bean.setInstrumentStatus(entity.getBackStatus());
            bean.setHospId(oldEntity.getHospId());
            busInstrumentDao.updateStatus(bean);
        }
        return i;
    }

    @Override
    public int updateHistory(BusRentEntity entity) {
        BusRentEntity oldEntity = busRentDao.searchById(entity.getRentId());
        int i = busRentDao.updateHistory(entity);
        if (StringUtils.isNotEmpty(entity.getBackStatus())) {//无归还状态，则为修改历史记录不保存设备基本信息
            BusInstrumentEntity bean = new BusInstrumentEntity();
            bean.setInstrumentId(oldEntity.getInstrumentId());
            bean.setIdCard(entity.getIdCard());
            bean.setInstrumentStatus(entity.getBackStatus());
            busInstrumentDao.updateStatus(bean);
        }
        //添加病人信息
        addPatient(entity);
        return i;
    }

    @Override
    public BusRentEntity searchById(Long id) {
        // TODO Auto-generated method stub
        if (id == null) {
            return null;
        }
        return busRentDao.searchById(id);
    }

    @Override
    public List<BusRentEntity> searchDue(BusRentEntity entity, PageParam pageParam) {

        HlHospitalEntity hospitalBean = commonMethod.getHospitalBean();
        if (hospitalBean != null) {
            entity.setHospId(hospitalBean.getId());
        }

        return busRentDao.searchDue(entity, pageParam);
    }

    @Override
    public int countDue(BusRentEntity entity) {
        HlHospitalEntity hospitalBean = commonMethod.getHospitalBean();
        if (hospitalBean != null) {
            entity.setHospId(hospitalBean.getId());
        }

        return busRentDao.countDue(entity);
    }

    @Override
    public BusRentEntity searchLastByInstrumentId(Long instrumentId) {
        return busRentDao.searchLastByInstrumentId(instrumentId);
    }

    @Override
    public Map<String, Object> rentCount(Long doctorId) {
        return busRentDao.rentCount(doctorId);
    }

    @Override
    public List<BusRentEntityVo> searchRentUsers(BusRentEntity entity, PageParam pageParam) {
        return busRentDao.searchRentUsers(entity, pageParam);
    }

    @Override
    public List<BusRentEntity> searchApp(BusRentEntity entity, PageParam pageParam) {
        return busRentDao.queryList(entity, pageParam);
    }

    @Override
    public Map countTimes(BusRentEntity entity) {
        return busRentDao.countTimes(entity);
    }


}
